//
//  QZMyAppointDeatilVc.m
//  WeiWeiHaircut
//
//  Created by 上官惠阳 on 15/12/11.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "QZMyAppointDeatilVc.h"
#import "QZMyAppiontDetailCell.h"
#import "QZMyOrderCell.h"
#import "QZLogisticCell.h"
#import "QZOrderBottomView.h"
#import "QZHairstyleViewController.h"
#import "MyAppointmentViewController.h"
#import "QZComplaintVc.h"
#import "QZStarViewCell.h"
#import "PayMoneyViewController.h"
#import "AppointmentModel.h"
@interface QZMyAppointDeatilVc ()<UITextFieldDelegate>
@property (nonatomic,strong) AppointmentModel *appoinModel;//最新的预约model
@end

@implementation QZMyAppointDeatilVc
{
    QZOrderBottomView *_bottomView;
    QZStarViewCell* _starStarView ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleWithSting:@"预约详情"];
    
    [self addBtnOnNavWithTitle:@"我的预约" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 80, 20) andDirection:left];
    if (self.model.display==2) {
        [self addBtnOnNavWithTitle:@"投诉" andImageName:nil andTarget:self andAction:@selector(complaintAction) andFrame:CGRectMake(0, 0, 35, 20) andDirection:right];
    }

    
    [self createTableViewWithPlainStyle:NO andSeparatorStyleNone:YES andBackGroudImageName:nil];
    
    [self registCellWithNib:@"QZMyOrderCell" addIdentifier:@"myOrderCell" addHeight:0];
    [self registCellWithNib:@"QZMyAppiontDetailCell" addIdentifier:@"myAppointDetailCell" addHeight:0];
    [self registCellWithNib:@"QZLogisticCell" addIdentifier:@"logisticsCell" addHeight:0];
    
    [self loadData];
    
    [self createFootView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self downloadDatas];
}
-(void)complaintAction{
    
    QZComplaintVc *camplainVc = [[QZComplaintVc alloc]init];
    camplainVc.model = _appoinModel;
    [self.navigationController pushViewController:camplainVc animated:YES];
}
-(void)createFootView
{
    _bottomView = [[[NSBundle mainBundle]loadNibNamed:@"QZOrderBottomView" owner:self options:nil] lastObject];
    _bottomView.frame = CGRectMake(0, HEIGHT - 48 - 64, WIDTH, 49);
    [_bottomView.bottomBtn setTitle:@"评价" forState:UIControlStateNormal];
    _bottomView.bottomLabel.text = @"服务完成后可评价";
    [_bottomView.bottomBtn addTarget:self action:@selector(judgeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bottomView];
    self.tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    
    
    //评星
    QZStarViewCell* starStarView = [[[NSBundle mainBundle]loadNibNamed:@"QZStarViewCell" owner:self options:nil] lastObject];
    _starStarView = starStarView;
    if (self.appoinModel.appoint_evaluate>0) {
        
        starStarView.starNumber = self.appoinModel.appoint_evaluate;
        starStarView.userInteractionEnabled = NO;
        _bottomView.hidden = YES;
    }
    starStarView.frame = CGRectMake(.0, .0, WIDTH, 150);
    self.tableView.tableFooterView = starStarView;
}
//评价
-(void)judgeAction
{
    if (_starStarView.starNumber<=0) {
        [[iToast makeText:@"请对本次服务评价"] show];
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"HairOrder.GetHairOrderAssess"];
    NSDictionary *dic =@{
                         @"order_id":@(self.appoinModel.id),
                         @"assess":@(_starStarView.starNumber)
                         };
    [self.request QZRequest_POST:url parameters:dic tagNSString:@"评价" stopRequest:NO isSerializer:NO isCache:NO];
}
-(void)loadData
{
    [self.tableSource addObject:@[@"",@""]];
//    [self.tableSource addObject:@[@{@"content":@"济南市中五部收件员 已揽件",@"time":@"2015-07-01 09:28"},
//                                  @{@"content":@"快件已到达 济南转运中心",@"time":@"2015-07-19 15:30"},
//                                  @{@"content":@"济南转运中心 已发出",@"time":@"2015-09-01 13:00"},
//                                  @{@"content":@"快件已到达 北京双井桥中通",@"time":@"2015-09-05 12:00"},
//                                  @{@"content":@"北京双井桥中通派件员正在为您派件",@"time":@"2015-09-20 11:30"},
//                                  @{@"content":@"快件已签收 签收人拍照签收",@"time":@"2015-09-21 16:11"}]];
    [self.tableView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 10;
    }
    return 0.0000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 198;
        }
        if (indexPath.row == 1) {
            return 55;
        }
    }
    
    return 70;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            QZMyOrderCell *myOrderCell = [tableView dequeueReusableCellWithIdentifier:@"myOrderCell"];
            myOrderCell.selectionStyle = UITableViewCellSelectionStyleNone;
            myOrderCell.contactBtn.hidden = NO;
            myOrderCell.overButton.hidden = NO;
            myOrderCell.model = self.appoinModel?self.appoinModel:self.model;
            [myOrderCell.overButton addTarget:self action:@selector(payClick:) forControlEvents:UIControlEventTouchUpInside];
            [myOrderCell.contactBtn addTarget:self action:@selector(telClick:) forControlEvents:UIControlEventTouchUpInside];
            [self startsIndex:self.appoinModel.display :myOrderCell];
            return myOrderCell;

        }
        if (indexPath.row == 1) {
            QZMyAppiontDetailCell *myAppointDetailCell = [tableView dequeueReusableCellWithIdentifier:@"myAppointDetailCell"];
            myAppointDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return myAppointDetailCell;
        }
    }
    if (indexPath.section == 1) {
        QZLogisticCell *logisticsCell = [tableView dequeueReusableCellWithIdentifier:@"logisticsCell"];
        logisticsCell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *arr = self.tableSource[1];
        if (indexPath.row == arr.count - 1) {
            logisticsCell.timeLabel.textColor = SBpurpleColor;
            logisticsCell.contentLabel.textColor = SBpurpleColor;
            logisticsCell.coverLine.hidden = NO;
            logisticsCell.coverPoint.hidden = NO;
        }else{
            logisticsCell.timeLabel.textColor = [UIColor darkGrayColor];
            logisticsCell.contentLabel.textColor = [UIColor darkGrayColor];
            logisticsCell.coverLine.hidden = YES;
            logisticsCell.coverPoint.hidden = YES;
        }
        NSDictionary *dic = arr[indexPath.row];
        logisticsCell.timeLabel.text = dic[@"time"];
        logisticsCell.contentLabel.text = dic[@"content"];
        
        return logisticsCell;
    }
    
    return nil;
}
-(void)startsIndex:(NSInteger)index :(QZMyOrderCell *)cell{
    NSString *string = nil;
    /*
     *contactBtn;//联系他们
     *overButton;//完成预约
     */
    _bottomView.hidden = YES;//先全部隐藏 评价按钮
    
    switch (index) {
        case 0:
            string = @"未付押金";
            cell.contactBtn.hidden = YES;
            cell.overButton.hidden = YES;
            _starStarView.hidden = YES;
            
            break;
        case 1:
            string = @"待服务";
            cell.contactBtn.hidden = NO;
            cell.overButton.hidden = YES;
            _starStarView.hidden = YES;
            break;
        case 2:
            string = @"已完成";
            cell.contactBtn.hidden = YES;
            cell.overButton.hidden = YES;
            _starStarView.hidden = NO;
            if (self.appoinModel.appoint_evaluate) {
                //显示星星
                _starStarView.userInteractionEnabled = NO;
                _starStarView.commitLabel.text = @"已评价";
                _bottomView.hidden = YES;//先全部隐藏 评价按钮
                _starStarView.starNumber = self.appoinModel.appoint_evaluate;
            }else{
                //待评价
                _starStarView.userInteractionEnabled = YES;
                _starStarView.commitLabel.text = @"请对本次服务评价";

                _bottomView.hidden = NO;//先全部隐藏 评价按钮
            }
            break;
        case 3:
            string = @"纠纷";
            cell.contactBtn.hidden = NO;
            cell.overButton.hidden = YES;
            _starStarView.hidden = NO;
            _starStarView.commitLabel.text = @"已评价";
            _starStarView.userInteractionEnabled = NO;
            _starStarView.starNumber = self.appoinModel.appoint_evaluate;
 
            break;
        case 4:
            string = @"未受理";
            cell.contactBtn.hidden = YES;
            cell.overButton.hidden = YES;
            _starStarView.hidden = YES;
            break;
        case 5:
            string = @"未支付";
            cell.overButton.hidden = NO;
            cell.contactBtn.hidden = NO;
            _starStarView.hidden = YES;
            break;
        default:
            break;
    }
   
    
}
-(void)payClick:(UIButton *)button{
    
   __block UITextField *textFi = nil;
    UIAlertController *con = [UIAlertController alertControllerWithTitle:@"请输入消费金额" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.delegate = self;
        textFi = textField;
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if ([textFi.text floatValue] > 0) {
            PayMoneyViewController *payCon = [[PayMoneyViewController alloc] initWithNibName:@"PayMoneyViewController" bundle:nil];
            
            payCon.payAppointMoney = YES;//是不是预约服务的钱
            payCon.isAppointment = YES;
            payCon.appModel = self.appoinModel;
            payCon.moneyServe = textFi.text;
            [self.navigationController pushViewController:payCon animated:YES];
        }else{
            [iToast makeText:@"金额不正确,请重新输入"];
        }

      
    }];
    
    [con addAction:action1];
    [con addAction:action];
    
    [self presentViewController:con animated:YES completion:nil];
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    return [self validateNumber:string];
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
-(void)telClick:(UIButton *)button{

    NSString *allString = [NSString stringWithFormat:@"tel:%@",self.appoinModel.shop_tel];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:allString]];
    NSLog(@"%@",allString);
    
}
-(void)actionAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(void)backAction{
    if(_isPayView){
        for (UIViewController *vc in self.navigationController.childViewControllers) {
            //再看是不是预约界面
            if ([vc class] == [MyAppointmentViewController class]) {
                [self.navigationController popToViewController:vc animated:YES];
                return;
            }
            //先看看有没有发型界面
            if ([vc class] == [QZHairstyleViewController class]) {
                [self.navigationController popToViewController:vc animated:YES];
                return;
            }
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)downloadDatas{
    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"HairOrder.GetHairOrderUserInfo"];
    [self.request QZRequest_POST:url parameters:@{@"order_id":@(self.model.id)} tagNSString:@"获取预约详情" stopRequest:NO isSerializer:NO isCache:YES];
    
    
}
-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag{
    self.appoinModel = nil;
    if ([tag isEqualToString:@"获取预约详情"]) {
        if ([value[@"data"][@"status"] integerValue] == 100) {
           NSDictionary *dic = value[@"data"][@"data"];
            
            self.appoinModel = [[AppointmentModel alloc] initWithDictionary:dic error:nil];
            [self.tableView reloadData];
        }
        
    }
    if ([tag isEqualToString:@"评价"]) {
        if ([value[@"data"][@"status"] integerValue] == 100) {
            [[iToast makeText:@"评价成功"] show];
            [self downloadDatas];
        }
        
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
